package com.livesource.client.web.projectBacklog;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.HTML;
import com.livesource.client.LiveSourceGlobalVariables;
import com.livesource.client.model.entry.ProjectClass;
import com.livesource.client.model.entry.ProjectFeature;
import com.livesource.client.utilities.FileExtensions;
import com.livesource.client.web.projectBacklog.edit.AceGWTEditor;
import com.livesource.client.web.projectBacklog.edit.FeatureForm;

/**
 * 
 * @Feature
 */
public class ProjectBacklogList extends FlexTable {

	public ProjectBacklogList() {

		this.setWidth("700px");

		setBorderWidth(1);

		setCellPadding(5);

		gridColumns();

		gridRows();
	}

	private void gridColumns() {

		this.setHTML(0, 0, "<B>Feature Name</B>");
		this.getColumnFormatter().setWidth(0, "150px");

		this.setHTML(0, 1, "<B>Created on</B>");
		this.getColumnFormatter().setWidth(1, "150px");

		this.setHTML(0, 2, "<B>Priority</B>");
		this.getColumnFormatter().setWidth(2, "60px");

		this.setHTML(0, 3, "<B>Estimate</B>");
		this.getColumnFormatter().setWidth(3, "60px");

		this.setHTML(0, 4, "<B>Status</B>");
		this.getColumnFormatter().setWidth(4, "60px");

		this.setHTML(0, 5, "");
		this.getColumnFormatter().setWidth(5, "50px");
	}

	private void gridRows() {

		int row = 0;
		for (ProjectClass entry : LiveSourceGlobalVariables.projectEntries
				.values()) {

			if (FileExtensions.EXTENSION_FEATURE.equals(entry
					.getFileExtension())) {

				row++;

				this.setWidget(row, 0, getName(entry));

				this.setText(row, 1, entry.getDateModified().toString());

				String priority = "";
				if (entry.getPriority() != null) {
					priority = entry.getPriority().toString();
				}
				this.setText(row, 2, priority);

				String estimate = "";
				if (entry.getEstimate() != null) {
					estimate = entry.getEstimate().toString();
				}
				this.setText(row, 3, estimate);

				this.setText(row, 4, entry.getStatus());

				this.setWidget(row, 5, iconSource(entry));
			}
		}
	}

	private HTML getName(final ProjectClass backlogEntry) {

		HTML htmlName = new HTML("<a href=#>" + backlogEntry.getFeatureName()
				+ "</a>");

		htmlName.addClickHandler(new ClickHandler() {

			@Override
			public void onClick(ClickEvent event) {

				LiveSourceGlobalVariables.showPanel.clear();
				LiveSourceGlobalVariables.showPanel.add(new FeatureForm(
						backlogEntry));
			}
		});

		return htmlName;
	}

	private static HTML iconSource(final ProjectFeature backlogEntry) {

		HTML img = new HTML("<a href='#'><img src='" + GWT.getModuleName()
				+ "/images/source.gif' ></a>");

		img.addClickHandler(new ClickHandler() {

			@Override
			public void onClick(ClickEvent event) {

				LiveSourceGlobalVariables.showPanel.clear();

				new AceGWTEditor().showCodeEditor((ProjectClass) backlogEntry);
			}
		});

		img.setTitle("View Source Code");

		return img;
	}
}
